package cn.wolfcode.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.domain.ArticleType;
import cn.wolfcode.service.IArticleTypeService;
import cn.wolfcode.vo.JSONMessage;

@Controller
@RequestMapping(value="/articleTypes")
public class ArticleTypeController {

	@Autowired
	private IArticleTypeService articleTypeService;
	
	/**
	 * 获取启用状态的文章分类
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public String articleTypeList(Model model) {
		List<ArticleType> articleTypeList = articleTypeService.getArticleTypeList();
		model.addAttribute("articleTypeList", articleTypeList);
		return "article/article_type_list";
	}
	
	
	/**
	 * 新增和修改文章分类
	 */
	@RequestMapping(value="",method=RequestMethod.POST)
	@ResponseBody
	public JSONMessage save(ArticleType acArticleType) {
		JSONMessage jSONMessage = new JSONMessage();
		try {
			articleTypeService.save(acArticleType);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			jSONMessage.setErrmsg(e.getMessage());
		}
		return jSONMessage;
	}
	
	/**
	 * 通过文章分类的ID删除对应的数据
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	@ResponseBody
	public JSONMessage delete(@PathVariable("id") Long id) {
		JSONMessage jSONMessage = new JSONMessage();
		try {
			articleTypeService.delete(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			jSONMessage.setErrmsg(e.getMessage());
		}
		return jSONMessage;
	}
}







